/* ====================================================================
 * (#)Header: JmsSender.java
 * (#)Revision: 
 * (#)Date Created: 02-Nov-09 10:32:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.jms;

import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;

/**
 * <code>JmsSender</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class JmsSender extends JmsBase {

	private MessageProducer producer;

	public JmsSender(String destinationJndiName) {
		super(destinationJndiName);
		try {
			setProducer(getSession().createProducer(getDestination()));
			getProducer().setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public JmsSender(Connection connection, String destinationJndiName) {
		super(connection, destinationJndiName);
		try {
			setProducer(getSession().createProducer(getDestination()));
			getProducer().setDeliveryMode(1);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void sendMessage(String text) throws JMSException {
		TextMessage message = getSession().createTextMessage();
		message.setText(text);
		getProducer().send(message);
	}

	public void sendMessage(Serializable object) throws JMSException {
		ObjectMessage message = getSession().createObjectMessage();
		message.setObject(object);
		getProducer().send(message);
	}

	@Override
	public void close() {
		if (getProducer() != null) {
			try {
				getProducer().close();
			} catch (Exception e) {
			}
		}
		super.close();
	}

	public MessageProducer getProducer() {
		return producer;
	}

	public void setProducer(MessageProducer producer) {
		this.producer = producer;
	}
}
